﻿using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

// Token: 0x02000307 RID: 775
public class MainController : MonoBehaviour
{
	// Token: 0x0600139D RID: 5021 RVA: 0x000837F0 File Offset: 0x000819F0
	public static void Reset()
	{
		if (MainController.mainController != null)
		{
			MainController.mainController.ResetScene();
		}
	}

	// Token: 0x0600139E RID: 5022 RVA: 0x0008380C File Offset: 0x00081A0C
	public void ResetScene()
	{
		this.sceneState = MainController.SceneState.Reset;
	}

	// Token: 0x0600139F RID: 5023 RVA: 0x00083815 File Offset: 0x00081A15
	public static void SwitchScene(string nextSceneName)
	{
		if (MainController.mainController != null && MainController.mainController.currentSceneName != nextSceneName)
		{
			MainController.mainController.nextSceneName = nextSceneName;
		}
	}

	// Token: 0x060013A0 RID: 5024 RVA: 0x00083848 File Offset: 0x00081A48
	protected void Awake()
	{
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		MainController.mainController = this;
		this.updateDelegates = new MainController.UpdateDelegate[7];
		this.updateDelegates[0] = new MainController.UpdateDelegate(this.UpdateSceneReset);
		this.updateDelegates[1] = new MainController.UpdateDelegate(this.UpdateScenePreload);
		this.updateDelegates[2] = new MainController.UpdateDelegate(this.UpdateSceneLoad);
		this.updateDelegates[3] = new MainController.UpdateDelegate(this.UpdateSceneUnload);
		this.updateDelegates[4] = new MainController.UpdateDelegate(this.UpdateScenePostload);
		this.updateDelegates[5] = new MainController.UpdateDelegate(this.UpdateSceneReady);
		this.updateDelegates[6] = new MainController.UpdateDelegate(this.UpdateSceneRun);
		this.nextSceneName = "MainMenu";
		this.sceneState = MainController.SceneState.Reset;
	}

	// Token: 0x060013A1 RID: 5025 RVA: 0x00083910 File Offset: 0x00081B10
	protected void Update()
	{
		if (this.updateDelegates[(int)this.sceneState] != null)
		{
			this.updateDelegates[(int)this.sceneState]();
		}
		if (this.loadingText)
		{
			this.loadingText.color = new Color(this.loadingText.color.r, this.loadingText.color.g, this.loadingText.color.b, Mathf.PingPong(Time.time, 1f));
		}
	}

	// Token: 0x060013A2 RID: 5026 RVA: 0x000839AC File Offset: 0x00081BAC
	protected void OnDestroy()
	{
		if (this.updateDelegates != null)
		{
			for (int i = 0; i < 7; i++)
			{
				this.updateDelegates[i] = null;
			}
			this.updateDelegates = null;
		}
		if (MainController.mainController != null)
		{
			MainController.mainController = null;
		}
	}

	// Token: 0x060013A3 RID: 5027 RVA: 0x000839FC File Offset: 0x00081BFC
	private void UpdateSceneReset()
	{
		GC.Collect();
		this.sceneState = MainController.SceneState.Preload;
	}

	// Token: 0x060013A4 RID: 5028 RVA: 0x00083A0A File Offset: 0x00081C0A
	private void UpdateScenePreload()
	{
		this.sceneLoadTask = SceneManager.LoadSceneAsync(this.nextSceneName);
		this.sceneState = MainController.SceneState.Load;
	}

	// Token: 0x060013A5 RID: 5029 RVA: 0x00083A24 File Offset: 0x00081C24
	private void UpdateSceneLoad()
	{
		if (this.sceneLoadTask.isDone)
		{
			this.sceneState = MainController.SceneState.Unload;
		}
	}

	// Token: 0x060013A6 RID: 5030 RVA: 0x00083A3D File Offset: 0x00081C3D
	private void UpdateSceneUnload()
	{
		if (this.resourceUnloadTask == null)
		{
			this.resourceUnloadTask = Resources.UnloadUnusedAssets();
		}
		else if (this.resourceUnloadTask.isDone)
		{
			this.resourceUnloadTask = null;
			this.sceneState = MainController.SceneState.Postload;
		}
	}

	// Token: 0x060013A7 RID: 5031 RVA: 0x00083A78 File Offset: 0x00081C78
	private void UpdateScenePostload()
	{
		this.currentSceneName = this.nextSceneName;
		this.sceneState = MainController.SceneState.Ready;
	}

	// Token: 0x060013A8 RID: 5032 RVA: 0x00083A8D File Offset: 0x00081C8D
	private void UpdateSceneReady()
	{
		GC.Collect();
		this.sceneState = MainController.SceneState.Run;
	}

	// Token: 0x060013A9 RID: 5033 RVA: 0x00083A9B File Offset: 0x00081C9B
	private void UpdateSceneRun()
	{
		if (this.currentSceneName != this.nextSceneName)
		{
			this.sceneState = MainController.SceneState.Reset;
		}
	}

	// Token: 0x040016ED RID: 5869
	public Text loadingText;

	// Token: 0x040016EE RID: 5870
	private static MainController mainController;

	// Token: 0x040016EF RID: 5871
	private string currentSceneName;

	// Token: 0x040016F0 RID: 5872
	private string nextSceneName;

	// Token: 0x040016F1 RID: 5873
	private AsyncOperation resourceUnloadTask;

	// Token: 0x040016F2 RID: 5874
	private AsyncOperation sceneLoadTask;

	// Token: 0x040016F3 RID: 5875
	private MainController.SceneState sceneState;

	// Token: 0x040016F4 RID: 5876
	private MainController.UpdateDelegate[] updateDelegates;

	// Token: 0x02000308 RID: 776
	private enum SceneState
	{
		// Token: 0x040016F6 RID: 5878
		Reset,
		// Token: 0x040016F7 RID: 5879
		Preload,
		// Token: 0x040016F8 RID: 5880
		Load,
		// Token: 0x040016F9 RID: 5881
		Unload,
		// Token: 0x040016FA RID: 5882
		Postload,
		// Token: 0x040016FB RID: 5883
		Ready,
		// Token: 0x040016FC RID: 5884
		Run,
		// Token: 0x040016FD RID: 5885
		Count
	}

	// Token: 0x02000309 RID: 777
	// (Invoke) Token: 0x060013AB RID: 5035
	private delegate void UpdateDelegate();
}
